<?php
/**
 * Posts.php
 * 
 * Based on WordPress 2.6.2
 * 
 * @category    Madoqua
 * @package     Module
 * @subpackage  Install
 */

/** base class */
require_once 'Import/Source/Abstract.php';

/**
 * Import_Source_Wordpress_Posts
 * 
 * Based on WordPress 2.6.2
 * 
 * @category    Madoqua
 * @package     Module
 * @subpackage  Install
 */
class Import_Source_Wordpress_Posts extends Import_Source_Abstract 
{
    protected $_name = 'wp_posts';
    
    /**
     * @var Blog_Comment
     */
    protected $_commentTable;
    
    public function init()
    {
        $this->_targetTable = new Blog_Post();
        $this->_commentTable = new Blog_Comment();
    }
    
    public function import()
    {
        /**
         * Posts:
         * 
         * WordPress  - Madoqua
         * --------------------
         * post_excerpt  - intro
         * post_name     - slug
         * post_title    - title
         * post_content  - content
         * user_id       - NA
         * post_date_gmt - posted (Conversion: MySQL GMT DATETIME column -> UNIX timestamp)
         * 
         * There are a whole host of columns in the WordPress posts table 
         * which are not listed here.
         * 
         */
        $posts = $this->fetchAll($this->select());
        
        foreach ($posts as $post) {
        	$row  = $this->_targetTable->createRow();
        	
        	$date = new Zend_Date($post->post_date_gmt);
        	
        	$row->slug   = $post->post_name;
        	$row->title  = $post->post_title;
        	$row->intro  = $post->post_excerpt;
        	$row->posted = $date->toValue(Zend_Date::TIMESTAMP);
        	
        	$postId = $row->save();
        	
        	$where = $this->getAdapter()->select()->from('wp_comments')
        	                                      ->where('comment_post_ID = ?', $post->ID);
        	$comments = $this->getAdapter()->fetchAll($where);
        	
        	/**
        	 * Comments:
        	 * 
        	 * WordPress - Madoqua
        	 * -------------------
        	 * comment_post_ID        - post_id (from $postId above)
        	 * 0 (assumed)            - spam
        	 * comment_approved       - accepted
        	 * posted                 - comment_date_gmt (Conversion: MySQL GMT DATETIME column -> UNIX timestamp)
        	 * comment_author         - name
        	 * comment_author_email   - email
        	 * comment_content        - content
        	 * comment_author_IP      - ip
        	 * NA                     - user_agent 
        	 */
        	foreach ($comments as $comment) {
        	    $row = $this->_commentTable->createRow();
        	    
        	    $date = new Zend_Date($comment->comment_date_gmt);
        	    
        	    $row->post_id = $postId;
        	    $row->spam = 0;
        	    $row->accepted = ($comment->comment_approved == 1) ? 1 : 0;
        	    $row->posted = $date->toValue(Zend_Date::TIMESTAMP);
        	    $row->name = $comment->comment_author;
        	    $row->email = $comment->comment_author_email;
        	    $row->content = $comment->comment_content;
        	    $row->ip = $comment->comment_author_IP;
        	    $row->user_agent = '';
        	    
        	    $row->save();
        	}
        }
    }
}
/*
For reference, this import is based on the following post table:
CREATE TABLE `wp_posts` (
  `ID` bigint(20) unsigned NOT NULL auto_increment,
  `post_author` bigint(20) NOT NULL default '0',
  `post_date` datetime NOT NULL default '0000-00-00 00:00:00',
  `post_date_gmt` datetime NOT NULL default '0000-00-00 00:00:00',
  `post_content` longtext NOT NULL,
  `post_title` text NOT NULL,
  `post_category` int(4) NOT NULL default '0',
  `post_excerpt` text NOT NULL,
  `post_status` varchar(20) NOT NULL default 'publish',
  `comment_status` varchar(20) NOT NULL default 'open',
  `ping_status` varchar(20) NOT NULL default 'open',
  `post_password` varchar(20) NOT NULL default '',
  `post_name` varchar(200) NOT NULL default '',
  `to_ping` text NOT NULL,
  `pinged` text NOT NULL,
  `post_modified` datetime NOT NULL default '0000-00-00 00:00:00',
  `post_modified_gmt` datetime NOT NULL default '0000-00-00 00:00:00',
  `post_content_filtered` text NOT NULL,
  `post_parent` bigint(20) NOT NULL default '0',
  `guid` varchar(255) NOT NULL default '',
  `menu_order` int(11) NOT NULL default '0',
  `post_type` varchar(20) NOT NULL default 'post',
  `post_mime_type` varchar(100) NOT NULL default '',
  `comment_count` bigint(20) NOT NULL default '0',
  PRIMARY KEY  (`ID`),
  KEY `post_name` (`post_name`),
  KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`)
) 

And the following comments table:

CREATE TABLE `wp_comments` (
  `comment_ID` bigint(20) unsigned NOT NULL auto_increment,
  `comment_post_ID` int(11) NOT NULL default '0',
  `comment_author` tinytext NOT NULL,
  `comment_author_email` varchar(100) NOT NULL default '',
  `comment_author_url` varchar(200) NOT NULL default '',
  `comment_author_IP` varchar(100) NOT NULL default '',
  `comment_date` datetime NOT NULL default '0000-00-00 00:00:00',
  `comment_date_gmt` datetime NOT NULL default '0000-00-00 00:00:00',
  `comment_content` text NOT NULL,
  `comment_karma` int(11) NOT NULL default '0',
  `comment_approved` varchar(20) NOT NULL default '1',
  `comment_agent` varchar(255) NOT NULL default '',
  `comment_type` varchar(20) NOT NULL default '',
  `comment_parent` bigint(20) NOT NULL default '0',
  `user_id` bigint(20) NOT NULL default '0',
  PRIMARY KEY  (`comment_ID`),
  KEY `comment_approved` (`comment_approved`),
  KEY `comment_post_ID` (`comment_post_ID`),
  KEY `comment_approved_date_gmt` (`comment_approved`,`comment_date_gmt`),
  KEY `comment_date_gmt` (`comment_date_gmt`)
)


 */